package com.shekhar.paas.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Order;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.shekhar.paas.domain.ArchivingJobLog;

@Controller
public class ArchivingJobLogController {

    @Autowired
    MongoTemplate mongoTemplate;
    
    
    @RequestMapping(method = RequestMethod.GET, value = "/logs")
    public String list(@RequestParam(value = "size", required = false) Integer size,Model uiModel) {
        if(size == null){
            size = Integer.valueOf(5);
        }
        Query query = new Query().limit(size);
        query.sort().on("startedAt", Order.DESCENDING);
        List<ArchivingJobLog> logs = mongoTemplate.find(query, ArchivingJobLog.class);
        uiModel.addAttribute("logs", logs);
        return "logs/list";
    }
}
